package com.js.nickcar.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.js.nickcar.R;
import com.js.nickcar.entity.remote.OptionBean;

import java.util.ArrayList;

/**
 * Created by LZY on 2017/6/20.
 */

public class OptionDialog extends Dialog implements View.OnClickListener  {


    private RecyclerView mRvInfo;
    private TextView mTvConfirm;
    private TextView mTvCancle;
    private OptionAdapter mAdapter;

    public OptionDialog(Context context) {
        this(context, R.style.dialog_wheel);
        // 让Dialog显示在底部
        getWindow().setGravity(Gravity.BOTTOM);
    }

    public OptionDialog(Context context, int themeResId) {
        super(context, themeResId);
        // 去掉标题，必须在setContentView之前调用
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View contentView = getLayoutInflater().inflate(getLayoutId(), null);
        initView(contentView);
        setContentView(contentView);
        // 设置Dialog的宽度等于屏幕的宽度，必须在setContentView后调用
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(p);
        getWindow().setWindowAnimations(R.style.AnimBottom);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        );
    }

    private int getLayoutId() {
        return R.layout.dialog_option;
    }

    @NonNull
    private void initView(View contentView) {
        mTvConfirm = (TextView) contentView.findViewById(R.id.tv_confirm);
        mTvCancle = (TextView) contentView.findViewById(R.id.tv_cancel);
        mRvInfo = (RecyclerView) contentView.findViewById(R.id.rv_info);
        mRvInfo.setLayoutManager(new GridLayoutManager(getContext(), 4));
        mAdapter = getAdapter();
        mRvInfo.setAdapter(mAdapter);
        mTvConfirm.setOnClickListener(this);
        mTvCancle.setOnClickListener(this);
    }

    private OptionAdapter getAdapter() {
        return new OptionAdapter(getContext());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_confirm:
                onConfirm();
                break;
            case R.id.tv_cancel:
                onCancle();
                break;
        }
    }

    /**
     * 取消
     */
    private void onCancle() {
        cancel();
    }

    private void onConfirm() {
        if (mAdapter.getCurOption() == null) {
            Toast.makeText(getContext().getApplicationContext(), "请选择匹配的内容", Toast.LENGTH_SHORT).show();
            return;
        }

        if (onConfirmListener != null) {
            onConfirmListener.onConfirm(mAdapter.getCurOption());
        }
        cancel();
    }

    private OnConfirmListener onConfirmListener;

    public OptionDialog setOnConfirmListener(OnConfirmListener onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
        return this;
    }

    public OptionDialog setData(ArrayList<OptionBean> options, OptionBean curOptionBean) {
        mAdapter.setCurOption(curOptionBean);
        mAdapter.addAll(options);
        return this;
    }

    public OptionDialog setSpanCount(int count) {
        mRvInfo.setLayoutManager(new GridLayoutManager(getContext(), count));
        return this;
    }

    public interface OnConfirmListener {
        void onConfirm(OptionBean option);
    }
}
